package cn.edu.ecut.ip;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class InetAddressTest {

	public static void main(String[] args) throws UnknownHostException {
		
		// 获得本地主机的IP地址
		InetAddress address = InetAddress.getLocalHost() ;
		System.out.println( address );
		System.out.println( "host name : " + address.getHostName() );
		System.out.println( "host address : " + address.getHostAddress() );
		
		byte[] bytes = address.getAddress();
		System.out.println( Arrays.toString( bytes ) );
		
		byte[] array = { (byte)192 , (byte)168 , (byte)150 , 1 };
		System.out.println( Arrays.toString( array ) );
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - -" );
		
		// 用 byte 数组来表示一个 IP地址 ( IPv4 )
		byte[] ipAdderss = { (byte)192 , (byte)168 , 0 , 100 };
		System.out.println( Arrays.toString( ipAdderss ) );
		
		// 根据 byte 数组得到一个 InetAddress 实例
		InetAddress inetAddress = InetAddress.getByAddress( ipAdderss );
		System.out.println( inetAddress );
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - -" );
		
		InetAddress[] addresses = InetAddress.getAllByName( InetAddress.getLocalHost().getHostName() );
		for (int i = 0; i < addresses.length; i++) {
			System.out.println( addresses[ i ] );
		}

	}

}
